import pandas as pd
import dolphindb as ddb
import time
import os


def process_data(start_date, end_date, csv_dir, output_dir):
    start_time = time.time()
    s = ddb.session("192.168.200.179", 8832, "chenzhimin", "suhhgjy98y_JHg87")

    basic_info_sql = f"""
        select secu_code, date(end_date) as trading_date, pre_close_price 
        from loadTable("dfs://ods_stock_quotation","ods_stock_quotation") 
        where if_trading_day = 1 and end_date >= {start_date} and end_date <= {end_date}
    """

    df_data = s.run(basic_info_sql)
    df_data['trading_date'] = pd.to_datetime(df_data['trading_date']).dt.strftime('%Y-%m-%d')

    start_year = pd.to_datetime(start_date).year
    end_year = pd.to_datetime(end_date).year

    for year in range(start_year, end_year + 1):
        csv_file = os.path.join(csv_dir, f'15min_{year}.csv')
        if os.path.exists(csv_file):
            combined_csv_df = pd.read_csv(csv_file)

            # 处理CSV数据
            combined_csv_df['trading_date'] = pd.to_datetime(combined_csv_df['日期']).dt.strftime('%Y-%m-%d')
            combined_csv_df = combined_csv_df.rename(columns={'代码': 'secu_code'})

            # 合并两个数据框7
            merged_df = pd.merge(combined_csv_df, df_data, on=['trading_date', 'secu_code'], how='inner')

            # 计算change_pct列
            merged_df['change_pct'] = (merged_df['avg_price'] - merged_df['pre_close_price']) / merged_df[
                'pre_close_price']

            # 保存结果
            output_file = os.path.join(output_dir, f'15min_{year}_result.csv')
            merged_df.to_csv(output_file, index=False)
            print(f"Result saved to {output_file}")
        else:
            print(f"File {csv_file} does not exist.")

    print(f"Processing completed in {time.time() - start_time} seconds")


# 循环从2014年到2024年
csv_dir = 'F:\\Personal\\data\\行情数据\\15min'
output_dir = 'F:\\Personal\\data\\行情数据\\15min_new'

for year in range(2014, 2025):
    start_date = f'{year}.01.01'
    end_date = f'{year}.12.31'
    process_data(start_date, end_date, csv_dir, output_dir)
